/*
 * @Description:
 * @Author: fhw
 * @Date: 2022-08-02 14:39:32
 */
import { ApiProperty } from '@nestjs/swagger'
import { BeforeInsert, Column, CreateDateColumn, Entity, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm'
import { GroupChat } from '../../group-chat/entities/group-chat.entity'
import { ChatUserFriend } from '../../chat_user_friends/entities/chat_user_friend.entity'
import { PaginatedResult } from '../../common/entities/entities'
import { SysMessage } from '../../sys_message/entities/sys_message.entity'
import { Resume } from 'src/resume/entities/resume.entity'
import { ChatUserGroup } from 'src/chat_user_group/entities/chat_user_group.entity'
import { hashSync } from 'bcryptjs'
import { Exclude } from 'class-transformer'
import { checkPassword } from 'src/utlis'

@Entity('chat_user')
export class ChatUser {
  @ApiProperty({ example: 'id', description: 'id' })
  @PrimaryGeneratedColumn({
    type: 'int',
    comment: 'id',
  })
  id: number | string

  @ApiProperty({ example: 'username', description: '用户名' })
  @Column({
    type: 'varchar',
    length: 100,
    comment: '用户名',
  })
  username: string

  @ApiProperty({ example: 'password', description: '密码' })
  @Exclude()
  @Column({
    type: 'varchar',
    length: 100,
    comment: '密码',
    select: false
  })
  password: string

  @BeforeInsert()
  async encryptPwd() {
    this.strength =  checkPassword(this.password)
    this.password = await hashSync(this.password);
   
  }

  @Column({
    type: 'enum',
    enum: ['低', '中', '高'],
    comment: '密码强度',
    select: false
  })
  strength: string

  @ApiProperty({ example: 'account ', description: '账号' })
  @Column({
    type: 'varchar',
    length: 100,
    comment: '账号',
    unique: true,
  })
  account: string

  @ApiProperty({ example: 'headimgurl', description: '头像' })
  @Column({
    type: 'text',
    comment: '头像',
    nullable: true,
  })
  headimgurl: string

  @ApiProperty({ example: 'signature', description: '签名' })
  @Column({
    type: 'varchar',
    length: 255,
    comment: '签名',
    nullable: true,
  })
  signature: string

  @ApiProperty({ example: 'sex', description: '性别' })
  @Column({
    comment: '性别',
    type: 'enum',
    enum: ['男', '女'],
  })
  sex: string

  @ApiProperty({ example: 'age', description: '年龄' })
  @Column({
    comment: '年龄',
    type: 'int',
    nullable: true
  })
  age: string

  @ApiProperty({ example: 'createDate', description: '注册日期' })
  @CreateDateColumn({
    type: 'datetime',
    comment: '注册日期',
  })
  createDate: string

  @ApiProperty({ example: 'login_date', description: '登录时间' })
  @Column({
    type: 'datetime',
    comment: '登录时间',
    nullable: true,
  })
  login_date: string

  @ApiProperty({ example: 'del', description: '账号状态：0禁用，1启用' })
  @Column({
    type: 'tinyint',
    comment: '账号状态：0禁用，1启用',
    default: 1,
  })
  del: number

  @ApiProperty({ example: 'friend', description: '好友' })
  @OneToMany(() => ChatUserFriend, (friend) => friend.user)
  friend?: ChatUserFriend[]

  @ApiProperty({ example: 'groupChat', description: '群聊' })
  @OneToMany(() => GroupChat, (group) => group.manager)
  groupChat?: GroupChat[]

  @ApiProperty({ example: 'chatUserGroup', description: '群聊' })
  @OneToMany(() => ChatUserGroup, (chatUserGroup) => chatUserGroup.chatUser)
  chatUserGroup?: ChatUserGroup[]
  // 消息
  @OneToMany(() => SysMessage, (m) => m.created)
  message: SysMessage

  // 简历
  @OneToOne(() => Resume, (resume) => resume.user)
  resume: Resume;
}

export class ChatUserResult extends PaginatedResult {
  @ApiProperty({
    example: ChatUser,
    type: [ChatUser],
  })
  data: ChatUser[]
  @ApiProperty({ example: 'pageSize', description: '数量' })
  pageSize: number
  @ApiProperty({ example: 'pageNum', description: '页数' })
  pageNum: number
  @ApiProperty({ example: 'total', description: '总数' })
  total: number
}
